import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

import { People } from '../../app/people';
import { PeopleService } from '../../app/people.service';
import { registURL } from '../../app/someConst';
import {Http, Headers, RequestOptions} from '@angular/http';

import 'rxjs/add/operator/map';

import { App } from 'ionic-angular';
import { PeopleDetailPage } from '../people-detail/people-detail';


/**
 * Generated class for the RegistPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-regist',
  templateUrl: 'regist.html',
})
export class RegistPage {

  userName: string;
  password: string;
  user: People;

  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              public peopleService: PeopleService,
              public http: Http,
              private app: App,) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegistPage');
  }

  registAction(){

    // alert('注册');
    if(!this.userName){
      alert('用户名为空');
      return;
    }

    if(!this.password){
      alert('密码为空');
      return;
    }

    let body = new FormData();
    body.append('user_name', this.userName);
    body.append('user_pwd', this.password);

    this.http.post(registURL, body).map(res => res.json()).subscribe(
      data => {
        console.log(data);
        if(data.code == 200){
          this.user = data.result as People;
          this.peopleService.people = this.user;
          this.app.getRootNav().setRoot(PeopleDetailPage); 
        } else {
          let message = data.message;
          alert(message);
          return;
        }
      },

      err => {
        console.log('ERROR', err);
      }

    );



    // if(this.peopleService.people){
    //   alert(this.peopleService.people);
    // } else {
    //   alert('对象为空');
    // }
  }

}
